﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DB_UpdateItem (smp_read_character_list, smp_read_item_equip_info)
    /// </summary>
    public class DB_UpdateItem : IDBProc
    {
        private Item m_item;
        public BaseObject m_CBObject = null;

        private enum DBFields : int
        {
            AccountId = 0,
        }

        public DB_UpdateItem(Item item)
        {
            m_item = item;
        }

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {
            DbConnection conn = GameDBManager.Instance.CreateConnection();
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "smp_update_item";
            cmd.CommandType = CommandType.StoredProcedure;

            DateTime dt = Globals.GetDateTimeFromUnixTimestamp((long)m_item.m_Instance.tElementalEffectExpire);

            DbParameter IN_SID = GameDBManager.Instance.CreateInParameter(cmd, "IN_SID", DbType.Int64, m_item.m_Instance.UID);
            DbParameter IN_SUMMON_ID = GameDBManager.Instance.CreateInParameter(cmd, "IN_SUMMON_ID", DbType.Int32, (int)m_item.m_Instance.nOwnSummonUID);
            DbParameter IN_IDX = GameDBManager.Instance.CreateInParameter(cmd, "IN_IDX", DbType.Int32, m_item.m_Instance.nIdx);
            DbParameter IN_FLAG = GameDBManager.Instance.CreateInParameter(cmd, "IN_FLAG", DbType.Int32, (int)m_item.m_Instance.Flag);
            DbParameter IN_COUNT = GameDBManager.Instance.CreateInParameter(cmd, "IN_COUNT", DbType.Int64, m_item.m_Instance.nCount);
            DbParameter IN_LEVEL = GameDBManager.Instance.CreateInParameter(cmd, "IN_LEVEL", DbType.Int32, m_item.m_Instance.nLevel);
            DbParameter IN_ENHANCE = GameDBManager.Instance.CreateInParameter(cmd, "IN_ENHANCE", DbType.Int32, m_item.m_Instance.nEnhance);
            DbParameter IN_ETHEREAL_DURABILITY = GameDBManager.Instance.CreateInParameter(cmd, "IN_ETHEREAL_DURABILITY", DbType.Int32, m_item.m_Instance.nDurability);
            DbParameter IN_ENDURANCE = GameDBManager.Instance.CreateInParameter(cmd, "IN_ENDURANCE", DbType.Int32, m_item.m_Instance.nCurrentEndurance);
            DbParameter IN_WEAR_INFO = GameDBManager.Instance.CreateInParameter(cmd, "IN_WEAR_INFO", DbType.Int32, (int)m_item.m_Instance.nWearInfo);
            DbParameter IN_SOCKET_0 = GameDBManager.Instance.CreateInParameter(cmd, "IN_SOCKET_0", DbType.Int32, (int)m_item.m_Instance.Socket[0]);
            DbParameter IN_SOCKET_1 = GameDBManager.Instance.CreateInParameter(cmd, "IN_SOCKET_1", DbType.Int32, (int)m_item.m_Instance.Socket[1]);
            DbParameter IN_SOCKET_2 = GameDBManager.Instance.CreateInParameter(cmd, "IN_SOCKET_2", DbType.Int32, (int)m_item.m_Instance.Socket[2]);
            DbParameter IN_SOCKET_3 = GameDBManager.Instance.CreateInParameter(cmd, "IN_SOCKET_3", DbType.Int32, (int)m_item.m_Instance.Socket[3]);
            DbParameter IN_REMAIN_TIME = GameDBManager.Instance.CreateInParameter(cmd, "IN_REMAIN_TIME", DbType.Int32, (int)m_item.m_Instance.tExpire);
            DbParameter IN_ELEMENTAL_EFFECT_TYPE = GameDBManager.Instance.CreateInParameter(cmd, "IN_ELEMENTAL_EFFECT_TYPE", DbType.Byte, (byte)m_item.m_Instance.eElementalEffectType);
            DbParameter IN_ELEMENTAL_EFFECT_EXPIRE_TIME = GameDBManager.Instance.CreateInParameter(cmd, "IN_ELEMENTAL_EFFECT_EXPIRE_TIME", DbType.DateTime, dt);
            DbParameter IN_ELEMENTAL_EFFECT_ATTACK_POINT = GameDBManager.Instance.CreateInParameter(cmd, "IN_ELEMENTAL_EFFECT_ATTACK_POINT", DbType.Int32, m_item.m_Instance.nElementalEffectAttackPoint);
            DbParameter IN_ELEMENTAL_EFFECT_MAGIC_POINT = GameDBManager.Instance.CreateInParameter(cmd, "IN_ELEMENTAL_EFFECT_MAGIC_POINT", DbType.Int32, m_item.m_Instance.nElementalEffectMagicPoint);

            conn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DB_UpdateItem.onProcess Exception: {0}", ex);
            }
            conn.Close();

            return true;
        }
    }

}
